fix: handle DROP TABLE CASCADE for dependent views in schema replay#4419
fix: handle DROP TABLE CASCADE for dependent views in schema replay#4419wucm667 wants to merge 1 commit into
Conversation
Signed-off-by: wucm667 <stevenwucongmin@gmail.com>
|
Would love for this to be considered - I believe it fixes my exact issue, so thank you @wucm667 |
|
I just closed #4456 in favor of this — I worked the same fix independently before finding yours. Same approach lands at the same place, so this is the one to merge. One thing I tried that's worth a quick consider: In const (
DropBehaviorUndefined DropBehavior = 0
DropBehaviorRestrict DropBehavior = 1
DropBehaviorCascade DropBehavior = 2
)and using Otherwise this looks solid — the RangeVar walk for view DependsOn is the natural place to capture deps, and the transitive eviction handles the views-on-views case I was worried about. |
|
@luongs3 Thanks for the kind words and for the suggestion! I agree the named constants @kyleconroy @jzelinskie Would love a review when you have a moment 🙂 |
|
Follow-up draft up at #4461 — just the named Will keep #4461 in draft until there's a signal either way. |
Problem
When
sqlc generatereplays a PostgreSQL schema containingDROP TABLE ... CASCADE, dependent views remain in the internal catalog. This causes subsequentCREATE VIEWstatements with the same name to fail withrelation "vw_reference_rates" already exists.Minimal repro (Playground):
In real Postgres,
DROP TABLE ... CASCADEdrops both the table and dependent views. In sqlc's replay, the view persists.Root Cause
ast.DropTableStmtlacked aBehaviorfield, so the parser never captured whetherCASCADEorRESTRICTwas specified.dropTable()in the catalog only removed the table and linked types — it never checked for or removed dependent views.Fix
1. Capture CASCADE/RESTRICT in the AST (
internal/sql/ast/drop_table_stmt.go)Added
Behavior DropBehaviorfield.2. Parser extracts Behavior (
internal/engine/postgresql/parse.go)When converting
DropStmtforOBJECT_TABLE,OBJECT_VIEW, orOBJECT_MATVIEW, pass throughn.Behavior.3. Track view dependencies (
internal/sql/catalog/view.go)Added
DependsOnTables []*ast.TableNametoTablestruct. ThecreateViewfunction now walks the SELECT query AST to extract allRangeVar(table reference) nodes, populating dependencies at view creation time.4. CASCADE drops dependent views (
internal/sql/catalog/table.go)When
Behavior == 2(DROP_CASCADEin pg_query_go protobuf),dropTablescans all tables/views in the schema and removes any that depend on the dropped table.5. Tests (
internal/engine/postgresql/catalog_test.go)TestDropTableCascadeViewRecreate: verifies CASCADE removes dependent views.TestDropTableCascadeWithoutCascadeFails: verifies without CASCADE, views remain in catalog.Testing
All existing
TestUpdateErrorscases pass; new CASCADE tests pass.Fixes #4416